package logic

import (
	"context"
	"edu-pro/edurpc/edurpc"
	"edu-pro/edurpc/internal/svc"
	"edu-pro/models/mysql"
	"edu-pro/pkg"
	"errors"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserLoginWithPwdLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUserLoginWithPwdLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserLoginWithPwdLogic {
	return &UserLoginWithPwdLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UserLoginWithPwdLogic) UserLoginWithPwd(in *edurpc.UserLoginWithPwdRequest) (*edurpc.UserLoginWithPwdResponse, error) {
	//•	手机号 + 密码登录
	var user mysql.User
	mysql.DB.Debug().Where("phone = ?", in.Phone).Find(&user)
	if user.ID == 0 {
		return nil, errors.New("该号码还未注册，请您先注册")
	}
	if user.Password != pkg.Md5string(in.Password) {
		return nil, errors.New("账号或密码有误")
	}
	if user.Status == 1 {
		return nil, errors.New("账号已禁用")
	}
	return &edurpc.UserLoginWithPwdResponse{
		UserId: int64(user.ID),
	}, nil
}
